<?php

require_once('functions/pageLoad.php');
loginRequired(true);
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['mid']) && isset($_POST['reply'])){

$sql = "INSERT INTO replies (message_id, reply_date, reply) VALUES ('".mysql_real_escape_string($_POST['mid'])."',NOW(),'".mysql_real_escape_string(stripslashes($_POST['reply']))."')";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

// send email to user

$sql = "SELECT messages.job_id, email, firstname, surname, trade, area FROM messages 
	LEFT JOIN jobs ON messages.job_id = jobs.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	LEFT JOIN users ON jobs.user_id = users.id
	WHERE messages.id = '".mysql_real_escape_string($_POST['mid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rs=mysql_fetch_assoc($query);

$job_link = build_job_link($user_rs['trade'],$user_rs['area'],$user_rs['job_id']);

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Message response notification";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nA tradesman has responded to your private message on I Want A Tradesman.\r\n\r\nYou can view the reponse here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

header("Location: ".$job_link."?result=reply");

}

?>